package com.edward.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.ContractItem;
import com.edward.model.ContractList;
import com.edward.model.PkList;
import com.edward.service.ContractItemManager;
import com.edward.service.ContractListManager;
import com.edward.service.PkListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PkListAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private ContractItemManager contractItemManager;
	
	private ContractListManager contractListManager;
	
	private PkListManager pkListManager;
	
	private PkList pkList;
	
	private ContractList contractList;
	
	private ContractItem contractItem;
	
	private String query;
	
	private int limit;
	
	private int start;
	
	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}
	
	public PkList getPkList() {
		return pkList;
	}

	public void setPkList(PkList pkList) {
		this.pkList = pkList;
	}

	public void setPkListManager(PkListManager pkListManager) {
		this.pkListManager = pkListManager;
	}

	public ContractList getContractList() {
		return contractList;
	}

	public void setContractList(ContractList contractList) {
		this.contractList = contractList;
	}

	public ContractItem getContractItem() {
		return contractItem;
	}

	public void setContractItem(ContractItem contractItem) {
		this.contractItem = contractItem;
	}
	

	public void setContractItemManager(ContractItemManager contractItemManager) {
		this.contractItemManager = contractItemManager;
	}

	public void setContractListManager(ContractListManager contractListManager) {
		this.contractListManager = contractListManager;
	}
	
	

	public PkListManager getPkListManager() {
		return pkListManager;
	}

	//添加pkList 的表头
	@Secured("ROLE_REPORT_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		pkListManager.addPkList(pkList);
		int total = pkListManager.listPkList().size();
		response.getWriter().print("{success : true , msg : '添加成功!',pkList_id:"+ pkList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		pkListManager.updatePkList(pkList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_REPORT_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		PkList p = pkListManager.getPkList(pkList.getId());
		pkListManager.deletePkList(p.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
	
		return null;
	}
	

	@Secured("ROLE_REPORT")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = pkListManager.pagerList(start, limit );
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	

	//获取grid 显示 的 分页信息

	@Secured("ROLE_REPORT")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(contractList.getListNum()!= null){
			pager = contractItemManager.pagerContractItem(start,limit,contractListManager.getContractList(contractList.getListNum()).getId());
		}else{
			pager = contractItemManager.pagerContractItem(start,limit,contractList.getId());
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_REPORT_ADD")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractItem c =  contractItemManager.getContractItem(contractItem.getId());
		System.out.println(pkList.getId());
		PkList pk = pkListManager.getPkList(pkList.getId());
		if(c.getGross() != 0){
		     pk.setTotal(pk.getTotal() - c.getGross() + contractItem.getPackages()*contractItem.getNet() );
		}else{
			   pk.setTotal(pk.getTotal() + contractItem.getPackages()*contractItem.getNet());
		}
		c.setPackages(contractItem.getPackages());
		c.setNet(contractItem.getNet());
		c.setGross(c.getNet() * c.getPackages());
		contractItemManager.updateContractItem(c);
		pkListManager.updatePkList(pk);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	public String getCmc() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		ContractList  c = contractListManager.getContractList(query);
		Map<String,Object> map = new HashMap<String,Object>();
		JSONObject jsonObject=null;
		map.put("f", c.getF());
		map.put("t", c.getT());
	    map.put("tel", c.getBtel());
		map.put("payment", c.getPayment());
		map.put("mes", c.getBuyer());
		 jsonObject = JSONObject.fromObject( map );
		
		response.getWriter().print(jsonObject.toString());
		return null;
	}
	
	
	@Secured("ROLE_REPORT")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	


	
	
	
	
	
	
	
	
	

}
